<?php if (!defined('ITPK')) exit('You can not directly access the file.');

/**
 * 机器人QQ操作类
 * @author 冬天的秘密
 * @link http://bbs.itpk.cn
 * @version 1.0
 */

class WebqqPer extends WebDBConnection {

	/**
	 * 获取数据库中所有的机器人QQ
	 * @return array | boolean
	 */
	public function getMeAll() {
		return $this->db->executeQuery("SELECT * FROM web_qq ORDER BY id ASC", true, true);
	}

	/**
	 * 根据QQ获取机器人属性
	 * @param string $uin
	 * @return array | boolean
	 */
	public function getMeByUin($uin) {
		return $this->db->executeQuery("SELECT * FROM web_qq WHERE uin = '{$uin}' LIMIT 1", true);
	}

	/**
	 * 根据QQ获取机器人属性
	 * @param string $secret
	 * @return array | boolean
	 */
	public function getMeBySecret($secret, $is_run = 1) {
		return $this->db->executeQuery("SELECT * FROM web_qq WHERE secret = '{$secret}' and is_run = {$is_run} LIMIT 1", true);
	}

	/**
	 * 根据是否开启了机器人功能获取QQ
	 * @param int $is_run 是否开启了机器人，默认为1（开启）
	 * @param int $limit 获取的个数，默认获取1个
	 * @return array | boolean
	 */
	public function getMe($is_run = 1, $limit = 1) {
		return $this->db->executeQuery("SELECT * FROM web_qq WHERE is_run = {$is_run} ORDER BY id ASC LIMIT {$limit}", true);
	}

	/**
	 * 根据机器人账号获取属性
	 * @param string $uin
	 * @param string $profile
	 * @return string
	 */
	public function getProfileByUin($uin, $profile = "name") {
		$qq = $this->db->executeQuery("SELECT {$profile} FROM web_qq WHERE uin = '{$uin}' LIMIT 1", true);
		return $qq["{$profile}"];
	}

	/**
	 * 获取机器人的回复状态
	 * @param string $uin
	 * @return int
	 */
	public function getIsReplyByUin($uin) {
		$qq = $this->db->executeQuery("SELECT is_reply FROM web_qq WHERE uin = '{$uin}' LIMIT 1", true);
		return $qq['is_reply'];
	}

	/**
	 * 获取机器人是否自动同意加群
	 * @param unknown $uin
	 * @return Ambigous <>
	 */
	public function getIsAutoGroupByUin($uin) {
		$qq = $this->db->executeQuery("SELECT is_auto_group FROM web_qq WHERE uin = '{$uin}' LIMIT 1", true);
		return $qq['is_auto_group'];
	}

	/**
	 * 获取创建者QQ
	 * @param string $uin
	 * @return Ambigous <>
	 */
	public function getCreateUinByUin($uin) {
		$qq = $this->db->executeQuery("SELECT create_uin FROM web_qq WHERE uin = '{$uin}' LIMIT 1", true);
		return $qq['create_uin'];
	}

	/**
	 * 更新String类型的值
	 * @param string $uin
	 * @param string $profile
	 * @param string $value
	 * @param int
	 */
	public function updateProfileToString($uin, $profile, $value) {
		return $this->db->executeQuery("UPDATE web_qq SET {$profile} = '{$value}' WHERE uin = '{$uin}'");
	}

	/**
	 * 更新Int类型的值
	 * @param string $uin
	 * @param string $profile
	 * @param string $value
	 * @return int
	 */
	public function updateProfileToInt($uin, $profile, $value) {
		return $this->db->executeQuery("UPDATE web_qq SET {$profile} = {$value} WHERE uin = '{$uin}'");
	}

	/**
	 * 清除登录痕迹
	 * @param string $uin
	 * @return int
	 */
	public function removeLoginRecord($uin) {
		return $this->db->executeQuery("UPDATE web_qq SET rsa_pass = '', code = '', verification = '' WHERE uin = '{$uin}'");
	}

	/**
	 * 根据QQ账号删除机器人
	 * @param string $uin
	 */
	public function deleteMe($uin) {
		$count = $this->db->executeQuery("DELETE FROM web_qq WHERE uin = '{$uin}'");
		return $count > 0 ? true : false;
	}

	/**
	 * 添加机器人账号
	 * @param string $uin
	 * @param string $pass
	 * @param string $name
	 * @param string $create_uin
	 */
	public function insertMe($uin, $pass, $name, $create_uin) {
		$secret = DataUtil::getRandString(8);
		$count = $this->db->executeQuery("INSERT INTO web_qq(uin, pass, name, create_uin, secret, createdate) VALUES('{$uin}', '{$pass}', '{$name}', '{$create_uin}', '{$secret}', " . time() . ")");
		return $count > 0 ? true : false;
	}

	/**
	 * 修改机器人账号
	 * @param string $uin
	 * @param string $pass
	 * @param string $name
	 * @param string $create_uin
	 * @param int $is_reply
	 * @param int $is_run
	 */
	public function updateMe($uin, $pass, $name, $create_uin, $is_reply, $is_auto_group, $is_run) {
		$count = $this->db->executeQuery("UPDATE web_qq SET pass = '{$pass}', name = '{$name}', create_uin = '{$create_uin}', is_reply = {$is_reply}, is_auto_group = {$is_auto_group}, is_run = {$is_run} WHERE uin = '{$uin}'");
		return $count > 0 ? true : false;
	}

} 
?>